package interfaces.hud.pilot;

import java.util.concurrent.Callable;

import org.fenggui.util.Color;

import com.jme.util.GameTaskQueueManager;

import logic.common.player.Player;
import logic.common.player.awards.PlayerAwardListener;
import logic.common.player.awards.PlayerGotAwardEvent;
import logic.common.player.playerListeners.HunterChangedEvent;
import logic.common.player.playerListeners.KillAssistEvent;
import logic.common.player.playerListeners.KillEvent;
import logic.common.player.playerListeners.MissionAssistEvent;
import logic.common.player.playerListeners.PlayerListener;
import logic.common.player.playerListeners.RespawnEvent;
import logic.common.player.playerListeners.TargetChangedEvent;
import logic.ships.hunter.Hunter;

import gameStates.absGamesStates.gui.AbsHUDState;
import interfaces.hud.BasicHUD;
import interfaces.hud.pilot.equipment.EquipmentHUD;
import interfaces.hud.pilot.status.HeatSeekerWarning;
import interfaces.hud.pilot.status.SpawnStationStatusHUD;
import interfaces.hud.targetInfos.AimPointer;
import interfaces.hud.targetInfos.TargetInfoWindow;

public class PilotHUD extends BasicHUD {
	
	protected EquipmentHUD equipmentWindow;
	protected HeatSeekerWarning heatSeekerWarning;
	protected AimPointer aimPointer;
	protected MissionContent missionContent;
	protected TargetInfoWindow targetInfoWindow;
	protected SpawnStationStatusHUD spawnStationHUD;
	protected MissionArrowHandler missionArrowHandler;
	protected AwardContent awardContent;
	protected TeamStatusWindow teamStatusWindow;
	
	public PilotHUD(AbsHUDState hudState) {
		super(hudState);
		
		targetInfoWindow = new TargetInfoWindow(this);
		addWidget(targetInfoWindow);
		
		boolean hasMissions = hudState.getIngameState().getGame().getGameSetup().hasMissions();
		if(hasMissions) {
			missionContent = new MissionContent(this);
			addWidget(missionContent);
		}
		
		addWidget(hunterStatusHUD);
		addWidget(counterMeasureHUD);
		
		final int disWidth = getWidth();
		
		try {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				@Override
				public Void call() throws Exception {
					
					int hswSize = disWidth / 12;
					int hswX = disWidth - (disWidth - hunterStatusHUD.getX() - hunterStatusHUD.getWidth()) / 2 - (hswSize / 2);
					int hswY = hunterStatusHUD.getHeight() * 3;
					
					heatSeekerWarning = new HeatSeekerWarning(hswX, hswY, hswSize, hswSize);
					return null;
				}
			});
		} catch(Exception e) { e.printStackTrace(); }
		
		Player player = game.getCurrentPlayer();
		aimPointer = new AimPointer(player);
		rootNode.attachChild(aimPointer);
		
		spawnStationHUD = new SpawnStationStatusHUD(this);
		
		equipmentWindow = new EquipmentHUD(player, this);
		addWidget(equipmentWindow);
		
		if(hasMissions) missionArrowHandler = new MissionArrowHandler(player, this);
		
		createInfoContent();
		
		createAwardContent();
		
		if(game.getGameSetup().hasTeamStatusWindow()) createTeamStatusWindow();
		
		addListener(player, new PlayerListener() {
			@Override
			public void respawned(RespawnEvent event) {
				respawn();
			}
			
			@Override
			public void hunterChanged(HunterChangedEvent event) {}
			@Override
			public void targetChanged(TargetChangedEvent event) {}
			@Override
			public void killed(KillEvent event) {}
			@Override
			public void killAssist(KillAssistEvent event) {}
			@Override
			public void missionAssist(MissionAssistEvent event) {}
		});
	}
	
	private void createTeamStatusWindow() {
		int width = getWidth() / 3;
		int height = getHeight() / 4;
		int y = gameHUD.getY() - height;
		teamStatusWindow = new TeamStatusWindow(this, game.getCurrentPlayer().getTeam(), width, height, y);
		addWidget(teamStatusWindow);
	}
	
	private void createAwardContent() {
		int size = getHeight() / 5;
		int x = (getWidth() / 2) - (size / 2);
		int y = getHeight() / 5 * 3;
		
		awardContent = new AwardContent(size, x, y);
		addWidget(awardContent);
		
		game.getCurrentPlayer().addPlayerAwardListener(new PlayerAwardListener() {
			@Override
			public void playerGotAward(PlayerGotAwardEvent event) {
				awardContent.addAward(event.getAward());
			}
		});
	}
	
	public void enableHeatSeekerWarning(boolean enable) {
		if(enable && !heatSeekerWarning.isEnabled()) addWidget(heatSeekerWarning);
		else if(!enable && heatSeekerWarning.isEnabled()) removeWidget(heatSeekerWarning);
		heatSeekerWarning.setEnabled(enable);
	}
	
	public TargetInfoWindow getTargetInfoWindow() { return targetInfoWindow; }
	
	protected void createInfoContent() {
		int width = getWidth() / 3;
		int height = getHeight() / 7;
		int x = getWidth() / 3;
		int y = hunterStatusHUD.getHeight();
		int widgetHeight = height/ 5;
		
		killInfoContent = new KillInfoContent(width, height, x, y, widgetHeight);
		addWidget(killInfoContent);
	}
	
	public void addToInfoContent(String[] text, Color[] colors, int money) {
		killInfoContent.add(text, colors, money);
	}
	
	public void toggleMissions() {
		if(missionContent != null) missionContent.toggleMissions();
	}
	
	protected void respawn() {
		equipmentWindow.respawn();
	}
	
	public void setAimPointerTarget(Hunter hunter) { aimPointer.setHunter(hunter); }
	
	public void enableAimPointer(boolean enable) { aimPointer.enablePointer(enable); }
}